<?php
namespace App\Http\Controllers\Business\ShopWeb\OrderManagement;

use App\Models\ShopUser;
use App\Models\ShopAccount;
use App\Models\Order;
use App\Models\User;
use App\Models\Shop;
use App\Models\ShopHistoryEvent;
use Illuminate\Support\Facades\Redis;

class OrderManagement {

    //订单列表
    public static function orderList($shopIds, $orderNumber='%', $minAmount=0.00, $maxAmount=9999, $shopUserId=0, $startTime='', $lastTime='', $status='%', $paginate=50, $shopAccountId=0) {
        if(empty($shopIds)) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        if($startTime == ''||$lastTime=='') {
            $startTime = '2016-07-01 00:00:00';
            $lastTime = date('Y-m-d') . " 23:59:59";
        }
        if($shopAccountId != 0) {
            //结款过来的
            $orderIdString = ShopAccount::whereIn('shopid', $shopIds)->where('id', $shopAccountId)->pluck('orderid')->first();
            if(!$orderIdString) {
                return ['status'=>'failed', 'message'=>'获取信息失败'];
            }
            $orderIds = explode(',', $orderIdString);
            $lists = Order::whereIn('id', $orderIds)->where('refundstatus', 0)
                ->orderBy('created_at', 'desc')
                ->select('id as orderid','created_at', 'ordernumber', 'userid', 'orderamount', 'accountstatus', 'shopid')
                ->paginate($paginate);
            foreach($lists as $k=>$v) {
                $lists[$k]['shopusername'] = '默认';
                $mobile = User::where('id', $lists[$k]['userid'])->pluck('mobile')->first();
                $lists[$k]['mobile'] = str_replace(substr($mobile, 3, 4), '****', $mobile);
                $lists[$k]['shopname'] = Shop::where('id', $lists[$k]['shopid'])->pluck('name')->first();
            }
            $orderCount = Order::whereIn('id', $orderIds)->where('refundstatus', 0)->get()->count();
            $sumAmount = Order::whereIn('id', $orderIds)->where('refundstatus', 0)->sum('orderamount');
        } else {
            if ($shopUserId == '-1') {
                //全部
                $lists = Order::whereIn('shopid', $shopIds)->where('ordernumber', 'like', $orderNumber)
                    ->where('orderamount', '>=', $minAmount)->where('orderamount', '<=', $maxAmount)
                    ->where('created_at', '>=', $startTime)->where('created_at', '<=', $lastTime)
                    ->where('status', 'like', $status)->where('refundstatus', 0)
                    ->orderBy('created_at', 'desc')
                    ->select('id as orderid', 'created_at', 'ordernumber', 'userid', 'orderamount', 'accountstatus', 'shopid', 'shopuserid')
                    ->paginate($paginate);
                foreach ($lists as $k => $v) {
                    if ($lists[$k]['shopuserid'] != 0) {
                        $lists[$k]['shopusername'] = ShopUser::where('id', $lists[$k]['shopuserid'])->pluck('name')->first();
                    } else {
                        $lists[$k]['shopusername'] = '默认';
                    }
                    $mobile = User::where('id', $lists[$k]['userid'])->pluck('mobile')->first();
                    $lists[$k]['mobile'] = str_replace(substr($mobile, 3, 4), '****', $mobile);
                    $lists[$k]['shopname'] = Shop::where('id', $lists[$k]['shopid'])->pluck('name')->first();
                }
                $orderCount = Order::whereIn('shopid', $shopIds)->where('ordernumber', 'like', $orderNumber)
                    ->where('orderamount', '>=', $minAmount)->where('orderamount', '<=', $maxAmount)
                    ->where('created_at', '>=', $startTime)->where('created_at', '<=', $lastTime)
                    ->where('status', 'like', $status)->where('refundstatus', 0)
                    ->orderBy('created_at', 'desc')->get()->count();
                $sumAmount = Order::whereIn('shopid', $shopIds)->where('ordernumber', 'like', $orderNumber)
                    ->where('orderamount', '>=', $minAmount)->where('orderamount', '<=', $maxAmount)
                    ->where('created_at', '>=', $startTime)->where('created_at', '<=', $lastTime)
                    ->where('status', 'like', $status)->where('refundstatus', 0)
                    ->orderBy('created_at', 'desc')->sum('orderamount');
            } elseif ($shopUserId == 0) {
                //默认
                $lists = Order::whereIn('shopid', $shopIds)->where('ordernumber', 'like', $orderNumber)
                    ->where('orderamount', '>=', $minAmount)->where('orderamount', '<=', $maxAmount)
                    ->where('created_at', '>=', $startTime)->where('created_at', '<=', $lastTime)
                    ->where('status', 'like', $status)->where('shopuserid', 0)->where('refundstatus', 0)
                    ->orderBy('created_at', 'desc')
                    ->select('id as orderid', 'created_at', 'ordernumber', 'userid', 'orderamount', 'accountstatus', 'shopid')
                    ->paginate($paginate);
                foreach ($lists as $k => $v) {
                    $lists[$k]['shopusername'] = '默认';
                    $mobile = User::where('id', $lists[$k]['userid'])->pluck('mobile')->first();
                    $lists[$k]['mobile'] = str_replace(substr($mobile, 3, 4), '****', $mobile);
                    $lists[$k]['shopname'] = Shop::where('id', $lists[$k]['shopid'])->pluck('name')->first();
                }
                $orderCount = Order::whereIn('shopid', $shopIds)->where('ordernumber', 'like', $orderNumber)
                    ->where('orderamount', '>=', $minAmount)->where('orderamount', '<=', $maxAmount)
                    ->where('created_at', '>=', $startTime)->where('created_at', '<=', $lastTime)
                    ->where('status', 'like', $status)->where('shopuserid', 0)->where('refundstatus', 0)
                    ->orderBy('created_at', 'desc')->get()->count();
                $sumAmount = Order::whereIn('shopid', $shopIds)->where('ordernumber', 'like', $orderNumber)
                    ->where('orderamount', '>=', $minAmount)->where('orderamount', '<=', $maxAmount)
                    ->where('created_at', '>=', $startTime)->where('created_at', '<=', $lastTime)
                    ->where('status', 'like', $status)->where('shopuserid', 0)->where('refundstatus', 0)
                    ->orderBy('created_at', 'desc')->sum('orderamount');
            } else {
                $lists = Order::whereIn('orders.shopid', $shopIds)->where('orders.ordernumber', 'like', $orderNumber)
                    ->where('orders.orderamount', '>=', $minAmount)->where('orders.orderamount', '<=', $maxAmount)
                    ->where('orders.created_at', '>=', $startTime)->where('orders.created_at', '<=', $lastTime)
                    ->where('orders.shopuserid', $shopUserId)->where('orders.status', 'like', $status)->where('refundstatus', 0)
                    ->join('shop_users', 'orders.shopid', '=', 'shop_users.id')
                    ->orderBy('orders.created_at', 'desc')
                    ->select('orders.id as orderid', 'orders.created_at', 'orders.ordernumber', 'orders.userid', 'orders.orderamount', 'orders.accountstatus', 'orders.shopid', 'shop_users.name as shopusername')
                    ->paginate($paginate);
                foreach ($lists as $k => $v) {
                    $mobile = User::where('id', $lists[$k]['userid'])->pluck('mobile')->first();
                    $lists[$k]['mobile'] = str_replace(substr($mobile, 3, 4), '****', $mobile);
                    $lists[$k]['shopname'] = Shop::where('id', $lists[$k]['shopid'])->pluck('name')->first();
                }
                $orderCount = Order::whereIn('shopid', $shopIds)->where('ordernumber', 'like', $orderNumber)
                    ->where('orderamount', '>=', $minAmount)->where('orderamount', '<=', $maxAmount)
                    ->where('created_at', '>=', $startTime)->where('created_at', '<=', $lastTime)
                    ->where('status', 'like', $status)->where('shopuserid', $shopUserId)->where('refundstatus', 0)
                    ->orderBy('created_at', 'desc')->get()->count();
                $sumAmount = Order::whereIn('shopid', $shopIds)->where('ordernumber', 'like', $orderNumber)
                    ->where('orderamount', '>=', $minAmount)->where('orderamount', '<=', $maxAmount)
                    ->where('created_at', '>=', $startTime)->where('created_at', '<=', $lastTime)
                    ->where('status', 'like', $status)->where('shopuserid', $shopUserId)->where('refundstatus', 0)
                    ->orderBy('created_at', 'desc')->sum('orderamount');
            }
        }
        $data = [
            'lists' => $lists,
            'count' => $orderCount,
            'sum' => $sumAmount
        ];
        return ['status'=>'success', 'data'=>$data];
    }
    //订单详情
    public static function getOrderDetail($shopIds, $orderId) {
        if(!$shopIds||!$orderId) {
            return ['status'=>'failed', 'message'=>'请求错误'];
        }
        $data = Order::where('orders.id', $orderId)->whereIn('orders.shopid', $shopIds)->where('orders.refundstatus', 0)
            ->join('users', 'orders.userid', '=', 'users.id')
            ->join('shops', 'orders.shopid', '=', 'shops.id')
            ->select('users.nickname', 'users.mobile', 'orders.orderamount', 'orders.accountamount', 'orders.ordernumber','orders.created_at', 'orders.path', 'orders.shopuserid', 'orders.accountstatus', 'orders.accounttime', 'shops.name as shopname')
            ->first();
        if(!$data) {
            return ['status'=>'failed', 'message'=>'请求错误'];
        }
        $data['mobile'] = str_replace(substr($data['mobile'], 3, 4), '****', $data['mobile']);
        if($data['shopuserid']) {
            $data['shopusername'] = ShopUser::where('id', $data['shopuserid'])->pluck('name')->first();
        } else {
            $data['shopusername'] = '默认';
        }
        return ['status'=>'success', 'data'=>$data];
    }
    //导出订单列表
    public static function exportOrderList($shopIds, $orderNumber='%', $minAmount=0.00, $maxAmount=9999, $shopUserId=0, $startTime='', $lastTime='', $status='%', $shopAccountId=0) {
        if(empty($shopIds)) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        if($startTime == ''||$lastTime=='') {
            $startTime = '2016-07-01 00:00:00';
            $lastTime = date('Y-m-d') . " 23:59:59";
        }
        if($shopAccountId != 0) {
            //全部
            $orderIdString = ShopAccount::whereIn('shopid', $shopIds)->where('id', $shopAccountId)->pluck('orderid')->first();
            if(!$orderIdString) {
                return ['status'=>'failed', 'message'=>'获取信息失败'];
            }
            $orderIds = explode(',', $orderIdString);
            $lists = Order::whereIn('id', $orderIds)->where('refundstatus', 0)
                ->select('id as orderid', 'created_at', 'ordernumber', 'userid', 'orderamount', 'accountstatus', 'shopid', 'shopuserid')
                ->get();
            foreach ($lists as $k => $v) {
                if ($lists[$k]['shopuserid'] == 0) {
                    $lists[$k]['shopusername'] = '默认';
                } else {
                    $lists[$k]['shopusername'] = ShopUser::where('id', $lists[$k]['shopuserid'])->pluck('name')->first();
                }
                $mobile = User::where('id', $lists[$k]['userid'])->pluck('mobile')->first();
                $lists[$k]['mobile'] = str_replace(substr($mobile, 3, 4), '****', $mobile);
                $lists[$k]['shopname'] = Shop::where('id', $lists[$k]['shopid'])->pluck('name')->first();
            }
        } else {
            if ($shopUserId == -1) {
                //全部
                $lists = Order::whereIn('shopid', $shopIds)->where('ordernumber', 'like', $orderNumber)
                    ->where('orderamount', '>=', $minAmount)->where('orderamount', '<=', $maxAmount)
                    ->where('created_at', '>=', $startTime)->where('created_at', '<=', $lastTime)
                    ->where('status', 'like', $status)->where('refundstatus', 0)
                    ->orderBy('created_at', 'desc')
                    ->select('id as orderid', 'created_at', 'ordernumber', 'userid', 'orderamount', 'accountstatus', 'shopid', 'shopuserid')
                    ->get();
                foreach ($lists as $k => $v) {
                    if ($lists[$k]['shopuserid'] == 0) {
                        $lists[$k]['shopusername'] = '默认';
                    } else {
                        $lists[$k]['shopusername'] = ShopUser::where('id', $lists[$k]['shopuserid'])->pluck('name')->first();
                    }
                    $mobile = User::where('id', $lists[$k]['userid'])->pluck('mobile')->first();
                    $lists[$k]['mobile'] = str_replace(substr($mobile, 3, 4), '****', $mobile);
                    $lists[$k]['shopname'] = Shop::where('id', $lists[$k]['shopid'])->pluck('name')->first();
                }
            } elseif ($shopUserId == 0) {
                //默认
                $lists = Order::whereIn('shopid', $shopIds)->where('ordernumber', 'like', $orderNumber)
                    ->where('orderamount', '>=', $minAmount)->where('orderamount', '<=', $maxAmount)
                    ->where('created_at', '>=', $startTime)->where('created_at', '<=', $lastTime)
                    ->where('status', 'like', $status)->where('shopuserid', 0)->where('refundstatus', 0)
                    ->orderBy('created_at', 'desc')
                    ->select('id as orderid', 'created_at', 'ordernumber', 'userid', 'orderamount', 'accountstatus', 'shopid')
                    ->get();
                foreach ($lists as $k => $v) {
                    $lists[$k]['shopusername'] = '默认';
                    $mobile = User::where('id', $lists[$k]['userid'])->pluck('mobile')->first();
                    $lists[$k]['mobile'] = str_replace(substr($mobile, 3, 4), '****', $mobile);
                    $lists[$k]['shopname'] = Shop::where('id', $lists[$k]['shopid'])->pluck('name')->first();
                }
            } else {
                $lists = Order::whereIn('orders.shopid', $shopIds)->where('orders.ordernumber', 'like', $orderNumber)
                    ->where('orders.orderamount', '>=', $minAmount)->where('orders.orderamount', '<=', $maxAmount)
                    ->where('orders.created_at', '>=', $startTime)->where('orders.created_at', '<=', $lastTime)
                    ->where('orders.shopuserid', $shopUserId)->where('orders.status', 'like', $status)->where('orders.refundstatus', 0)
                    ->join('shop_users', 'orders.shopid', '=', 'shop_users.id')
                    ->orderBy('orders.created_at', 'desc')
                    ->select('orders.id as orderid', 'orders.created_at', 'orders.ordernumber', 'orders.userid', 'orders.orderamount', 'orders.accountstatus', 'orders.shopid', 'shop_users.name as shopusername')
                    ->get();
                foreach ($lists as $k => $v) {
                    $mobile = User::where('id', $lists[$k]['userid'])->pluck('mobile')->first();
                    $lists[$k]['mobile'] = str_replace(substr($mobile, 3, 4), '****', $mobile);
                    $lists[$k]['shopname'] = Shop::where('id', $lists[$k]['shopid'])->pluck('name')->first();
                }
            }
        }
        return ['status'=>'success', 'data'=>$lists];
    }
    //绘制商家最近七天的翻倍日的订单数
    public static function chartLastEventOrderCount($shopId, $days=7) {
        if(!$shopId || !$days) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        $xLabel = []; //时间轴
        $yLabel = []; //订单轴
        $count = $days;
        $isShow = 0;
        $eventLists = ShopHistoryEvent::where('shopid', $shopId)->select('event')->orderBy('event', 'desc')->skip(0)->take($days)->get();
        foreach($eventLists as $eventList) {
            if($count==0) {
                break;
            }
            $list = $eventList['event'];
            $startTime = $list . ' 00:00:00';
            $lastTime = $list . ' 23:59:59';
            $orderCount = Order::where('shopid', $shopId)->where('refundstatus', 0)->where('created_at', '>=', $startTime)->where('created_at', '<=', $lastTime)->count();
            array_push($xLabel, [$count, date('m-d', strtotime($list))]);
            array_push($yLabel, [$count, $orderCount]);
            if($orderCount > 0) {
                $isShow = 1;
            }
            --$count;
        }
        $data = ['xlabel' => $xLabel, 'ylabel' => $yLabel, 'isshow'=>$isShow];
        return ['status'=>'success', 'data'=>$data ];
    }
}